package com.check.operation.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.base.BaseActivity;
import com.check.operation.base.BaseFragment;
import com.check.operation.fragment.HjgyFragment_;
import com.check.operation.fragment.InfoFragment;
import com.check.operation.fragment.JrfssFragment_;
import com.check.operation.fragment.LksssFragment_;
import com.check.operation.fragment.ShFragment_;
import com.check.operation.fragment.SqFragment_;
import com.check.operation.fragment.SstzFragment_;
import com.check.operation.fragment.SzFragment_;
import com.check.operation.fragment.YzzxFragment_;
import com.check.operation.model.User;
import com.check.operation.model.UserRe;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.szj.util.JSONUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

@Fullscreen
@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {
    public static final Integer CHECK_CODE = 110;
    public static final Integer CHECK_RESULT_CODE = 100;

    @App CheckApplication checkApp;

    @ViewById(R.id.mDrawerLayout) DrawerLayout mDrawerLayout;
    @ViewById(R.id.mNavigationView) NavigationView mNavigationView;
    @ViewById(R.id.navigation) BottomNavigationView mBottomNavigationView;
    @ViewById(R.id.frameLayout) FrameLayout mFrameLayout;

    @ViewById TextView yhzh;
    @ViewById TextView xtrq;
    @ViewById TextView dqmk;
    @ViewById TextClock xtsj;

    @StringRes(R.string.version) String versionStr;

    private Fragment currentFragment;

    private RelativeLayout header_layout;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.navigation_fs:
                            //复苏
                            replaceFragment("fs");
                            return true;
                        case R.id.navigation_more:
                            //更多
                            mDrawerLayout.openDrawer(Gravity.LEFT);
                            return true;
                        case R.id.navigation_sh:
                            //术中
                            replaceFragment("sh");
                            return true;
                        case R.id.navigation_sq:
                            //术前
                            replaceFragment("sq");
                            return true;
                        case R.id.navigation_sz:
                            //术中
                            replaceFragment("sz");
                            return true;
                        default:
                    }
                    return false;
                }
            };

    @AfterViews
    public void initView() {
        UserRe re = checkApp.getUserRe();
        yhzh.setText(re.getUser().EmpName);
        xtrq.setText(JSONUtil.getNYR(checkApp.getDate()));
        header_layout = (RelativeLayout) mNavigationView.getHeaderView(0);

        initHeaderView();
        mNavigationView.setNavigationItemSelectedListener(this);
//        mNavigationView.setItemIconTintList(null);
        /*去掉navigation中的滑动条*/
        mNavigationView.getChildAt(0).setVerticalScrollBarEnabled(false);
        mNavigationView.setItemTextAppearance(R.style.mMenuTextStyle);

        mBottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        replaceFragment("info");
    }

    @Override
    public void onGetBarValue(String barcode) {
        if(currentFragment != null && currentFragment instanceof BaseFragment) {
            ((BaseFragment) currentFragment).onGetBarValue(barcode);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        currentFragment.onActivityResult(requestCode, resultCode, data);
    }

    @UiThread
    public void uidqmk(String mkmc){
        dqmk.setText(mkmc);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.item_hjgy:
                //呼叫工友
                replaceFragment("hjgy");
                break;
            case R.id.item_lksss:
                //离开手术室
                replaceFragment("lksss");
                break;
            case R.id.item_sstz:
                //手术通知
                replaceFragment("sstz");
                break;
            case R.id.item_yzzx:
                //医嘱执行
                replaceFragment("yzzx");
                break;
            case R.id.item_sq:
                //进入术前室
                replaceFragment("sq");
                break;
            case R.id.item_sz:
                //术中
                replaceFragment("sz");
                break;
            case R.id.item_sh:
                //术后
                replaceFragment("sh");
                break;
            case R.id.item_fs:
                //进入复苏室
                replaceFragment("fs");
                break;
            default:
        }
        mDrawerLayout.closeDrawers();
        return false;
    }

    protected void replaceFragment(String tag) {
        try {
            if(currentFragment != null && currentFragment.getTag().equals(tag)) {
                return;
            }
            if (currentFragment != null) {
                getSupportFragmentManager().beginTransaction().detach(currentFragment).commit();
            }
            currentFragment = getSupportFragmentManager().findFragmentByTag(tag);
            String title = "";
            if (currentFragment == null) {
                switch (tag) {
                    case "sq":
                        title = "进入术前室";
                        currentFragment = new SqFragment_();
                        break;
                    case "sz":
                        title = "术中";
                        currentFragment = new SzFragment_();
                        break;
                    case "sh":
                        title = "术后";
                        currentFragment = new ShFragment_();
                        break;
                    case "fs":
                        title = "进入复苏室";
                        currentFragment = new JrfssFragment_();
                        break;
                    case "hjgy":
                        title = "呼叫工友";
                        currentFragment = new HjgyFragment_();
                        break;
                    case "lksss":
                        title = "离开手术室";
                        currentFragment = new LksssFragment_();
                        break;
                    case "sstz":
                        title = "手术通知";
                        currentFragment = new SstzFragment_();
                        break;
                    case "yzzx":
                        title = "医嘱执行";
                        currentFragment = new YzzxFragment_();
                        break;
                    case "info":
                        title = "主页";
                        currentFragment = new InfoFragment();
                        break;
                    default:
                        currentFragment = new InfoFragment();
                }
                getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, currentFragment, tag).commit();
            } else {
                getSupportFragmentManager().beginTransaction().attach(currentFragment).commit();
            }
            uidqmk(title);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initHeaderView() {
        TextView name = (TextView) header_layout.findViewById(R.id.text_name);
        TextView dept = (TextView) header_layout.findViewById(R.id.text_dept);
        TextView version = (TextView) header_layout.findViewById(R.id.text_version);

        User user = checkApp.getUserRe().getUser();
        name.setText(user.EmpName);
        dept.setText(user.DeptName);
//        version.setText(versionStr);
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(MainActivity.this)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(-1);
                    }
                })
                .setTitle("退出？")
                .setMessage("确认退出系统？")
                .create()
                .show();
    }
}
